<?php
/**
 * @user: thanatos <thanatos915@163.com>
 */

namespace frontend\controllers;


use common\models\City;
use common\models\Share;
use frontend\models\ShareForm;
use yii\base\Exception;
use yii\helpers\Json;
use yii\web\Controller;

class ShareController extends Controller
{
    public $layout = 'share';

    public function actionIndex()
    {
        $cityModel = City::find()->all();
        return $this->render('index', ['city' => $cityModel]);
    }

    public function actionSubmit()
    {
        $post = \Yii::$app->request->post();
        $model = new ShareForm();
        if ($result = $model->submit($post)) {
            $data = [
                'status' => 'y',
                'data' => $result->toArray()
            ];
        } else {
            $data = [
                'status' => 'n',
                'msg' => $model->getFirstError('')
            ];
        }
        return Json::encode($data);
    }

    /**
     * @param $id
     * @throws Exception
     * @author thanatos <thanatos915@163.com>
     */
    public function actionShow($id)
    {
        $model = Share::findOne(['id' => $id]);
        if (empty($model)) {
            throw new Exception('参数错误');
        }

        return $this->render('show', ['model' => $model]);
    }

}